﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.Common;

namespace Copad.BusinessCard.DAL
{
    public class ModelInfo
    {
        private string insertSQL = null;
        private string deleteByIDSQL = null;
        private string deleteSQL = null;
        private string updateSQL = null;
        private string selectSQL = null;
        private string selectByIDSQL = null;
        private string selectCountSQL = null;

        public ModelInfo()
        {
            PropertyInfoList = new List<PropertyInfo>();
        }

        public Type ModelType { get; set; }
        public string TableName { get; set; }
        public List<PropertyInfo> PropertyInfoList { get; private set; }

        public string InsertSQL
        {
            get
            {
                if (insertSQL == null)
                {
                    string sql = "insert into " + TableName + "(";
                    PropertyInfoList.ForEach(x =>
                    {
                        if (x.Name != "ID") { sql += x.Name + ","; }
                    });
                    sql = sql.Remove(sql.Length - 1) + ") values(";
                    PropertyInfoList.ForEach(x =>
                    {
                        if (x.Name != "ID") { sql += "@" + x.Name + ","; }
                    });
                    insertSQL = sql.Remove(sql.Length - 1) + ");select @@IDENTITY;";
                }
                return insertSQL;
            }
        }

        public string DeleteByIDSQL
        {
            get
            {
                if (deleteByIDSQL == null)
                {
                    deleteByIDSQL = "delete from " + TableName + " where ID=@ID";
                }
                return deleteByIDSQL;
            }
        }

        public string DeleteSQL
        {
            get
            {
                if (deleteSQL == null)
                {
                    deleteSQL = "delete from " + TableName;
                }
                return deleteSQL;
            }
        }

        public string UpdateSQL
        {
            get
            {
                if (updateSQL == null)
                {
                    string sql = "update " + TableName + " set ";
                    PropertyInfoList.ForEach(x =>
                    {
                        if (x.Name != "ID")
                        {
                            sql += x.Name + "=@" + x.Name + ",";
                        }
                    });


                    updateSQL = sql.Remove(sql.Length - 1) + " where id=@id";
                }
                return updateSQL;
            }
        }

        public string SelectSQL
        {
            get
            {
                if (selectSQL == null)
                {
                    selectSQL = "select * from " + TableName;
                }
                return selectSQL;
            }
        }

        public string SelectCountSQL
        {
            get
            {
                if (selectCountSQL == null)
                {
                    selectCountSQL = "select count(*) from " + TableName;
                }
                return selectCountSQL;
            }
        }

        public string SelectByIDSQL
        {
            get
            {
                if (selectByIDSQL == null)
                {
                    selectByIDSQL = "select * from " + TableName + " where id=@id ";
                }
                return selectByIDSQL;
            }
        }
    }
}
